package study04;

import java.util.HashSet;

public class Java08_hashset_object {
    public static void main(String[] args) {
        HashSet set = new HashSet();

        User8 user1 = new User8();
        user1.id = 1001;
        user1.name = "zs";
        User8 user2 = new User8();
        user2.id = 1001;
        user2.name = "zs";
        User8 user3 = new User8();
        user3.id = 1002;
        user3.name = "ls";

        set.add(user1);
        set.add(user2);
        set.add(user3);

        System.out.println(set);
    }
}

class User8 {
    public int id;
    public String name;

    @Override
//    类似内存地址
    public int hashCode() {
        return id;
    }

    @Override
//    判断两个对象属性是否相同
    public boolean equals(Object obj) {
        if (obj instanceof User8) {
            User8 otherUser = (User8) obj;
            if (otherUser.id == this.id) {
                if (otherUser.name.equals(this.name)) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return "User[" + id + "," + name + "]";
    }
}
